package com.sky.service.impl;


import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;


@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Transactional
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //先查询购物车中是否存在该商品
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //获取当前线程登陆的用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //存在，只增加数量
        if (list != null && list.size() != 0){
            //这里的list中只可能查出一条数据
            ShoppingCart shoppingCart1 = list.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber()+1);
            shoppingCartMapper.updateNumberById(shoppingCart1);
        }else {
            //不存在，插入新的购物车数据
            //判断是菜品还是套餐
            if (shoppingCart.getDishId() != null){
                //获取对应的菜品信息
                Dish dish = dishMapper.getById(shoppingCart.getDishId());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
            }else {
                //获取对应的套餐信息
                Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
            }
            //公共字段
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            //插入数据
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     */
    @Override
    public List<ShoppingCart> select() {
        //获取当前登录的用户id
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> list = shoppingCartMapper.selectByUserId(userId);
        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        //获取当前登录的用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }

    /**
     * 删除购物车中一个商品
     */
    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //先判断删除的是菜品还是套餐
        if (shoppingCartDTO.getDishId() != null){
            //删除的是菜品
            List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);
            if (shoppingCarts != null && shoppingCarts.size() != 0){
                //查询出对应的菜品,判断数量是否>1
                ShoppingCart shoppingCart1 = shoppingCarts.get(0);
                if (shoppingCart1.getNumber()>1){
                    shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
                    shoppingCartMapper.updateNumberById(shoppingCart1);
                }else {
                    shoppingCartMapper.deleteById(shoppingCart1);
                }
            }
        }else {
            //删除的是套餐
            List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);
            if (shoppingCarts != null && shoppingCarts.size() != 0){
                //查询对应的套餐,判断数量是否>1
                ShoppingCart shoppingCart1 = shoppingCarts.get(0);
                if (shoppingCart1.getNumber()>1){
                    shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
                    shoppingCartMapper.updateNumberById(shoppingCart1);
                }else {
                    shoppingCartMapper.deleteById(shoppingCart1);
                }
            }

        }
    }
}
